package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import top.rainf.constants.AttributeConstants;
import top.rainf.service.IReportService;
import top.rainf.support.Result;
import top.rainf.domain.vo.Report;
import top.rainf.support.TokenGenerator;

import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description 举报的控制器类 由本人的生成器自动生成~
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    IReportService reportService;
    /**
    * 获取全部举报的列表
    * @return 返回是否获取成功~
    */
    @RequestMapping("/list")
    public Result list(){
        List<Report> list = reportService.getReportList();
        if(list == null){
            return Result.fail();
        }
        return Result.success().addObj("list",list);
    }

    /**
    * 根据id获取一个举报
    * @return 返回是否获取成功~
    */
    @RequestMapping("/one")
    public Result getOneReport(int id){
        Report report = reportService.getOneById(id);
        if(report == null){
                    return Result.fail();
        }
        return Result.success().addObj("data",report);
    }
    /**
    * 根据id删除一个举报
    ---------------------------
    * @param id 要删除的举报的id
    * @return 返回是否获取成功~
    */
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        boolean suc = reportService.deleteOneById(id);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(List<Integer> idList){
        boolean suc = reportService.deleteAll(idList);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("update")
    public Result update(Report report){
        boolean suc = reportService.updateById(report);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/addOne")
    public Result addOne(Report report,
                         @SessionAttribute(AttributeConstants.LOGIN_TOKEN) String token){
        int uid = TokenGenerator.getUserIdFromToken(token);
        if(uid == -1){
            return Result.fail("请先登录");
        }
        report.setReporterId(uid);
        if (reportService.addOne(report)) {
            return Result.success("举报成功");
        }
        return Result.fail("举报失败");
    }
}
